<?php 
namespace frontend\controllers;

use Yii;
use yii\web\Controller;
use PDO;
/**
 * @author  xutao <[email address]>
 * @date  2017-11-28
 * @brief mysql用户管理
 */
class UserController extends Controller
{
	//禁止yii框架的安全验证
	public $enableCsrfValidation=false;
	/*
	 * 禁用yii样式
	 */
	public $layout = false;
	/**
	 * pdo链接方法
	 * @param  [type] $sql [description]
	 * @return [type]      [description]
	 */
	public function pdo($k)
	{
        $dbinfos = Yii::$app->session->get('dbinfos');
        $dbinfos_arr = unserialize($dbinfos);
        $one_dbinfo = $dbinfos_arr[$k];
        $dbh = new PDO("mysql:host=$one_dbinfo[1];port=$one_dbinfo[2];charset=utf8", "$one_dbinfo[3]", "$one_dbinfo[4]");
        return $dbh;
	}
	/**
	 * 用户显示
	 * @return [type] [description]
	 */
	public function actionIndex()
	{	
		$k = Yii::$app->request->get('k');
        $pdo = $this->pdo($k);
        $sql = "select * from mysql.user";
        $table = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
		return $this->render('user_index',[
			'user'=>$table,
			'key'	  => $k
		]);
	}

	/**
	 * 用户详情信息
	 * @return [type] [description]
	 */
	public function actionSelect()
	{
		$k = Yii::$app->request->get('k');
		$host = Yii::$app->request->get('host');
		$user = Yii::$app->request->get('user');
        $pdo = $this->pdo($k);
		$sql = "select * from mysql.user where Host='$host' and User='$user'";
		$data = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
		$list = $data[0];
		return $this->render('user_select',['list'=>$list]);
	}

	/**
	 * 用户删除
	 * @return [type] [description]
	 */
	public function actionDeletes()
	{
		$k = Yii::$app->request->post('k');
		$pdo = $this->pdo($k);
		$host = Yii::$app->request->post('host');
		$user = Yii::$app->request->post('user');
		$sql = "delete from mysql.user where Host='$host' and User='$user'";
		$pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
		return 1;
	}
	/**
	 * 用户创建页面
	 * @return [type] [description]
	 */
	public function actionInsert()
	{
		$k = Yii::$app->request->get('k');
		return $this->render('user_insert', [
			'k' => $k
		]);
	}
	/**
	 * 用户创建
	 * @return [type] [description]
	 */
	public function actionInsertin()
	{
		$k = Yii::$app->request->post('k');
		// print_r($k);die;
		$pdo = $this->pdo($k);
		$name = Yii::$app->request->post('name');
		$mainframe = Yii::$app->request->post('mainframe');
		$pass = Yii::$app->request->post('password');
		$word = Yii::$app->request->post('word');
		$obj = Yii::$app->request->post('obj');
		if($obj==''){
			$sql = "CREATE USER '".$name."'@'".$mainframe."' IDENTIFIED BY '".$pass."';";
			$pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
			return '0';
        }else{
        	$asd = substr($obj,0,-1);
			$str = '';
			$sql1 = "CREATE USER '".$name."'@'".$mainframe."' IDENTIFIED BY '".$pass."';GRANT ";
			$sql2 = " ON *.* TO '".$name."'@'".$mainframe."'";
			$sql = $sql1.$asd.$sql2;
			// print_r($sql);die;
			$pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
			return '0';
        }
	}
	/**
	 * 用户权限修改
	 * @return [type] [description]
	 */
	public function actionUpdate()
	{
		$k = Yii::$app->request->get('k');
		$pdo = $this->pdo($k);
		$host = Yii::$app->request->get('host');
		$user = Yii::$app->request->get('user');
		$sql = "select * from mysql.user where Host='$host' and User='$user'";
		$data = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
		$list = $data[0];
		return $this->render('user_update',['list'=>$list]);
	}
}
?>